<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class MchWithdraw extends Model{
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_mch.name as mch_name";
        $list = $this
            ->field($field)
            ->join('think_mch',"think_mch.id = {$this->getTable()}.mc_id")
            ->order($this->getTable().'.create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     *
     * @param $name
     * @param $money
     * @param int $mc_id
     * @param string $out
     * @return $this|bool
     */
    public function addNew($name='',$money=0,$mc_id=0,&$out=''){
        $mch = new Mch();
        $info = $mch->get($mc_id);
        if(!$info){
            $out = '商户不存在';
            return false;
        }
        $_res = $this->check($mc_id,$out1);
        if(!$_res){
            $out = $out1;
            return false;
        }
        if(!$name){
            $name = "商户【{$info['name']}】，申请提现";
        }
        if(!$money){
            $out = '请填写提现金额';
            return false;
        }
        if($money<1 || $money>1000){
            $out = '提现金额在1-1000元之间';
            return false;
        }
        if($money>$info['money']){
            $out = '账户余额不足';
            return false;
        }
        $valid = new \app\admin\validate\MchWithdraw();
        $data = [
            'name'=>$name,
            'mc_id'=>$mc_id,
            'money'=>$money,
            'status'=>1,
            'order_id'=>build_order_no(),
            'update_time'=>0
        ];
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        $this->data($data);
        return $this;
    }

    /**
     * 检测提现
     * @param int $id
     * @param string $out
     * @return bool
     */
    public function check($id=0,&$out=''){
        $start = date('Y-m-d',time());
        $end = date('Y-m-d',time());
        $count1 = $this
            ->where('mc_id','eq',$id)
            ->where('status','eq',1)
            ->count();
        if($count1){
            $out = '您有一个提现待处理，请耐心等待处理';
            return false;
        }
        $count = $this
            ->whereBetweenTime('create_time',"{$start} 00:00:00","{$end} 23:59:59")
            ->where('mc_id','eq',$id)
            ->whereIn('status','2,3,4,-1')
            ->count();
        if($count){
            $out = '一天内只能提现一次,请明日再来';
            return false;
        }
        return true;
    }

    /**
     * 更新商户提现
     * @param int $order_id
     * @param string $desc
     * @param string $sent
     * @param string $hblist
     * @param null $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function decMoneyByOrderId($order_id=0,$desc='',$sent='',$hblist='',&$out=null){
        $info = $this->where('order_id','eq',$order_id)->find();
        if(!$info){
            $out = '提现不存在';
            return false;
        }
        $this->startTrans();
        $mch = (new Mch())->get($info['proxy_id']);
        if($info['status']!=2){
            $mch->dong_money = $mch['money'] - $info['money'];
            $info->status = 2;
            $info->update_time = time();
            $info->info = $desc;
            $info->sent_info = $sent;
            $info->hblist = $hblist?json_encode($hblist):'';
            $result = $info->save();
            $result2 = $mch->save();
        }else{
            $result = $info->sent_info = $sent;
            $result2 = true;
        }
        if($result && $result2){
            $this->commit();
            return true;
        }else{
            $this->rollback();
            $out = '领取失败';
            return false;
        }
    }


    /**
     * @param $value
     * @return string
     */
    protected function getFailInfoAttr($value){
        return $value?$value:'--';
    }
    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    protected function getUpdateTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

    /**
     * @param $value
     * @return string
     */
    protected function getSetInfoAttr($value){
        return $value?$value:'--';
    }
}